<HTML>
<HEAD>
<TITLE>[Chapter 3] Fonts and Colors</TITLE>
<META NAME="author" CONTENT="John Zukowski">
<META NAME="date" CONTENT="Thu Jul 31 14:27:12 1997">
<META NAME="form" CONTENT="html">
<META NAME="metadata" CONTENT="dublincore.0.1">
<META NAME="objecttype" CONTENT="book part">
<META NAME="otheragent" CONTENT="gmat dbtohtml">
<META NAME="publisher" CONTENT="O'Reilly &amp; Associates, Inc.">
<META NAME="source" CONTENT="SGML">
<META NAME="subject" CONTENT="Java AWT">
<META NAME="title" CONTENT="Java AWT">
<META HTTP-EQUIV="Content-Script-Type" CONTENT="text/javascript">
</HEAD>
<body vlink="#551a8b" alink="#ff0000" text="#000000" bgcolor="#FFFFFF" link="#0000ee">

<DIV CLASS=htmlnav>
<H1><a href='index.htm'><IMG SRC="gifs/smbanner.gif"
     ALT="Java AWT" border=0></a></H1>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch02_08.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><B><FONT FACE="ARIEL,HELVETICA,HELV,SANSERIF" SIZE="-1">Chapter 3</FONT></B></TD>
<td width=172 align=right valign=top><A HREF="ch03_02.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
</table>

&nbsp;
<hr align=left width=515>
</DIV>
<H1 CLASS=chapter><A CLASS="TITLE" NAME="JAWT-CH-3">3. Fonts and Colors</A></H1>

<DIV CLASS=htmltoc>

<p>
<b>Contents:</b><br>
Fonts<br>
<A HREF="ch03_02.htm">FontMetrics</A><BR>
<A HREF="ch03_03.htm">Color</A><BR>
<A HREF="ch03_04.htm">SystemColor</A><BR>
<A HREF="ch03_05.htm">Displaying Colors</A><BR>
<A HREF="ch03_06.htm">Using Desktop Colors</A><BR>

<p>
</DIV>

<P CLASS=para>
This chapter introduces the <tt CLASS=literal>java.awt</tt> 
classes that are used to work with different fonts and colors. First, we 
discuss the <tt CLASS=literal>Font</tt> class, which 
determines the font used to display text strings, whether they are drawn 
directly on the screen (with <tt CLASS=literal>drawString()</tt>) 
or displayed within a component like a text field. The <tt CLASS=literal>FontMetrics</tt> 
class gives you detailed information about a font, which you can use to 
position text strings intelligently. Next, the <tt CLASS=literal>Color</tt> 
class is used to represent colors and can be used to specify the background 
color of any object, as well as the foreground color used to display a 
text string or a shape. Finally, the <tt CLASS=literal>SystemColor</tt> 
class (which is new to Java 1.1) provides access to the desktop color scheme. 

<DIV CLASS=sect1>
<h2 CLASS=sect1><A CLASS="TITLE" NAME="JAWT-CH-3-SECT-1">3.1 Fonts</A></h2>

<P CLASS=para>
<A NAME="CH03.FONT"></A>An instance of the <tt CLASS=literal>Font</tt> class 
represents a specific font to the system. Within AWT, a font is specified 
by its name, style, and point size. Each platform that supports Java provides 
a basic set of fonts; to find the fonts supported on any platform, call 
<tt CLASS=literal>Toolkit.getDefaultToolkit().getFontList()</tt>. 
This method returns a <tt CLASS=literal>String</tt> 
array of the fonts available. Under Java 1.0, on any platform, the available fonts were: TimesRoman, Helvetica, Courier, Dialog, DialogInput, and ZapfDingbats. For copyright reasons, the list is substantially different in Java 1.1: the available font names are TimesRoman <img src="gifs/wstar.gif" alt="(Deprecated)" border=0>, Serif, Helvetica <img src="gifs/wstar.gif" alt="(Deprecated)" border=0>, SansSerif, Courier <img src="gifs/wstar.gif" alt="(Deprecated)" border=0>, Monospaced, Dialog, and DialogInput. The actual fonts available aren't changing; the deprecated font names are being replaced by non-copyrighted equivalents. Thus, TimesRoman is now Serif, Helvetica is now SansSerif, and Courier is Monospaced. The ZapfDingbats font name has been dropped completely because the characters in this font have official Unicode mappings in the range \u2700 to \u27ff.

<DIV CLASS=note>
<P CLASS=note><BLOCKQUOTE><P><B>NOTE:</B> 
</blockquote><P>
</DIV>

<P CLASS=para>
If you desire non-Latin font support with Java 1.1, use the Unicode mappings for the characters. The actual font used is specified in a set of <I CLASS=emphasis>font.properties</I> files in the <I CLASS=emphasis>lib</I> subdirectory under <I CLASS=emphasis>java.home</I>. These localized font files allow you to remap the "Serif", "SansSerif", and "Monospaced" names to different fonts.
</blockquote><P>
</DIV>

<P CLASS=para>
The font's <tt CLASS=literal>style</tt> is passed 
with the help of the class variables <tt CLASS=literal>Font.PLAIN</tt>, 
<tt CLASS=literal>Font.BOLD</tt>, and <tt CLASS=literal>Font.ITALIC</tt>. 
The combination <tt CLASS=literal>Font.BOLD | Font.ITALIC</tt> 
specifies bold italics. 

<P CLASS=para>
A font's <tt CLASS=literal>size</tt> is represented 
as an integer. This integer is commonly thought of as a point size; although 
that's not strictly correct, this book follows common usage and talks 
about font sizes in points. 

<P CLASS=para>
It is possible to add additional font names to the system by setting properties. 
For example, putting the line below in the properties file or a resource 
file (resource files are new to Java 1.1) defines the name "AvantGarde" 
as an alias for the font SansSerif: 

<P CLASS=para>
<DIV CLASS=screen>
<P>
<PRE>
awt.font.avantgarde=SansSerif
</PRE>
</DIV>

<P CLASS=para>
With this line in the properties file, a Java program can use "AvantGarde" 
as a font name; when this font is selected, AWT uses the font SansSerif 
for display. The property name must be all lowercase. Note that we haven't 
actually added a new font to the system; we've only created a new 
name for an old font. See the discussion of <tt CLASS=literal>getFont()</tt> 
and <tt CLASS=literal>decode()</tt> for more on font 
properties. 

<DIV CLASS=sect2>
<h3 CLASS=sect2><A CLASS="TITLE" NAME="JAWT-CH-3-SECT-1.1">The Font Class</A></h3><A NAME="CH03.FONT2"></A>Constants

<P CLASS=para>
There are four styles for displaying fonts in Java: plain, bold, italic, 
and bold italic. Three class constants are used to represent font styles: 

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><I CLASS=emphasis>public static final int BOLD </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>BOLD</tt> constant represents 
a boldface font. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public static final int ITALIC </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>ITALIC</tt> constant represents 
an italic font. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public static final int PLAIN </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>PLAIN</tt> constant represents 
a plain or normal font. </DL>
<P CLASS=para>
The combination <tt CLASS=literal>BOLD | ITALIC</tt> 
represents a bold italic font. <tt CLASS=literal>PLAIN</tt> 
combined with either <tt CLASS=literal>BOLD</tt> or 
<tt CLASS=literal>ITALIC</tt> represents bold or italic, 
respectively. 

<P CLASS=para>
There is no style for underlined text. If you want underlining, you have 
to do it manually, with the help of <tt CLASS=literal>FontMetrics</tt>. 

<DIV CLASS=note>
<P CLASS=note><BLOCKQUOTE><P><B>NOTE:</B> 
</blockquote><P>
</DIV>

<P CLASS=para>
If you are using Microsoft's SDK, the <tt CLASS=literal>com.ms.awt.FontX</tt> 
class includes direct support for underlined, strike through (line through 
middle), and outline fonts. 
</blockquote><P>
</DIV>

Variables

<P CLASS=para>
Three protected variables access the font setting. They are initially set 
through the <tt CLASS=literal>Font</tt> constructor. 
To read these variables, use the <tt CLASS=literal>Font</tt> 
class's "get" methods. 

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><I CLASS=emphasis>protected String name </I><br>
<DD>

<P CLASS=para>
The name of the font. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>protected int size </I><br>
<DD>

<P CLASS=para>
The size of the font. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>protected int style </I><br>
<DD>

<P CLASS=para>
The style of the font. The style is some logical combination of the constants 
listed previously. </DL>
Constructors

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><I CLASS=emphasis>public Font (String name, int style, int size) </I><br>
<DD>

<P CLASS=para>
There is a single constructor for <tt CLASS=literal>Font</tt>. 
It requires a <tt CLASS=literal>name</tt>, <tt CLASS=literal>style</tt>, 
and <tt CLASS=literal>size</tt>. <tt CLASS=literal>name</tt> 
represents the name of the font to create, case insensitive. </DL>
<P CLASS=para>
<DIV CLASS=screen>
<P>
<PRE>
setFont (new Font ("TimesRoman", Font.BOLD | Font.ITALIC, 20));
</PRE>
</DIV>

Characteristics

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><I CLASS=emphasis>public String getName () </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>getName()</tt> method returns 
the font's logical name. This is the name passed to the constructor 
for the specific instance of the <tt CLASS=literal>Font</tt>. 
Remember that system properties can be used to alias font names, so the 
name used in the constructor isn't necessarily the actual name of 
a font on the system. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public String getFamily () </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>getFamily()</tt> method returns 
the actual name of the font that is being used to display characters. If 
the font has been aliased to another font, the <tt CLASS=literal>getFamily()</tt> 
method returns the name of the platform-specific font, not the alias. For example, 
if the constructor was <tt CLASS=literal>new Font (</tt>"<tt CLASS=literal>AvantGarde</tt>"<tt CLASS=literal>, 
Font.PLAIN, 10)</tt> and the <tt CLASS=literal>awt.font.avantgarde=Helvetica</tt> 
property is set, then <tt CLASS=literal>getName()</tt> 
returns AvantGarde, and <tt CLASS=literal>getFamily()</tt> 
returns Helvetica. If nobody set the property, both methods return AvantGarde, 
and the system uses the default font (since AvantGarde is a nonstandard 
font). 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public int getStyle () </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>getStyle()</tt> method returns 
the current style of the font as an integer. Compare this value with the 
constants <tt CLASS=literal>Font.BOLD</tt>, <tt CLASS=literal>Font.PLAIN</tt>, 
and <tt CLASS=literal>Font.ITALIC</tt> to see which 
style is meant. It is easier to use the <tt CLASS=literal>isPlain()</tt>, 
<tt CLASS=literal>isBold()</tt>, and <tt CLASS=literal>isItalic()</tt> 
methods to find out the current style. <tt CLASS=literal>getStyle()</tt> 
is more useful if you want to copy the style of some font when creating 
another. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public int getSize () </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>getSize()</tt> method retrieves 
the point size of the font, as set by the size parameter in the constructor. 
The actual displayed size may be different. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public FontPeer getPeer () <img src="gifs/bstar.gif" alt="(New)" border=0> </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>getPeer()</tt> method retrieves 
the platform-specific peer object. The object <tt CLASS=literal>FontPeer</tt> 
is a platform-specific subclass of <tt CLASS=literal>sun.awt.PlatformFont</tt>. 
For example, on a Windows 95 platform, this would be an instance of <tt CLASS=literal>sun.awt.windows.WFontPeer</tt>. </DL>
Styles

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><I CLASS=emphasis>public boolean isPlain () </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>isPlain()</tt> method returns 
<tt CLASS=literal>true</tt> if the current font is 
neither bold nor italic. Otherwise, it returns <tt CLASS=literal>false</tt>. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public boolean isBold () </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>isBold()</tt> method returns 
<tt CLASS=literal>true</tt> if the current font is 
either bold or bold and italic. Otherwise, it returns <tt CLASS=literal>false</tt>. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public boolean isItalic () </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>isItalic()</tt> method returns 
<tt CLASS=literal>true</tt> if the current font is 
either italic or bold and italic. Otherwise, it returns <tt CLASS=literal>false</tt>. </DL>
Font properties

<P CLASS=para>
<A NAME="CH03.PROP"></A>Earlier, you saw how to use system properties to add aliases for fonts. 
In addition to adding aliases, you can use system properties to specify 
which fonts your program will use when it runs. This allows your users 
to customize their environments to their liking; your program reads the 
font settings at run-time, rather than using hard-coded settings. The format 
of the settings in a properties file is: 

<P CLASS=para>
<DIV CLASS=screen>
<P>
<PRE>
propname=fontname--style--size
</PRE>
</DIV>

<P CLASS=para>
where <tt CLASS=literal>propname</tt> is the name 
of the property being set, <tt CLASS=literal>fontname</tt> 
is any valid font name (including aliases), <tt CLASS=literal>style</tt> 
is <tt CLASS=literal>plain</tt>, <tt CLASS=literal>bold</tt>, 
<tt CLASS=literal>italic</tt>, or <tt CLASS=literal>bolditalic</tt>, 
and <tt CLASS=literal>size</tt> represents the desired 
size for the font. <tt CLASS=literal>style</tt> and 
<tt CLASS=literal>size</tt> default to <tt CLASS=literal>plain</tt> 
and 12 points. Order is important; the font's style must always precede 
its size. 

<P CLASS=para>
For example, let's say you have three areas on your screen: one for 
menus, one for labels, and one for input. In the system properties, you 
allow users to set three properties: <tt CLASS=literal>myPackage.myClass.menuFont</tt>, 
<tt CLASS=literal>myPackage.myClass.labelFont</tt>, 
and <tt CLASS=literal>myPackage.myClass.inputFont</tt>. 
One user sets two: 

<P CLASS=para>
<DIV CLASS=screen>
<P>
<PRE>
myPackage.myClass.menuFont=TimesRoman-italic-24
myPackage.myClass.inputFont=Helvetica
</PRE>
</DIV>

<P CLASS=para>
The user has specified a Times font for menus and Helvetica for other 
input. The property names are up to the developer. The program uses <tt CLASS=literal>getFont()</tt> 
to read the properties and set the fonts accordingly. 

<DIV CLASS=note>
<P CLASS=note><BLOCKQUOTE><P><B>NOTE:</B> 
</blockquote><P>
</DIV>

<P CLASS=para>
The location of the system properties file depends on the run-time 
environment and version you are using. Normally, the file goes into a subdirectory 
of the installation directory, or for environments where users have home 
directories, in a subdirectory for the user. Sun's HotJava, JDK, 
and <I CLASS=emphasis>appletviewer</I> tools use the <I CLASS=emphasis>properties</I> 
file in the <I CLASS=emphasis>.hotjava</I> 
directory. 

<P CLASS=para>
Most browsers do not permit modifying properties, so there is no 
file. 

<P CLASS=para>
Java 1.1 adds the idea of "resource files," which are 
syntactically similar to properties files. Resource files are then placed 
on the server or within a directory found in the <tt CLASS=literal>CLASSPATH</tt>. 
Updating the properties file is no longer recommended. 
</blockquote><P>
</DIV>

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><I CLASS=emphasis>public static Font getFont (String name) </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>getFont()</tt> method gets the 
font specified by the system property <tt CLASS=literal>name</tt>. 
If <tt CLASS=literal>name</tt> is not a valid system 
property, <tt CLASS=literal>null</tt> is returned. 
This method is implemented by a call to the next version of <tt CLASS=literal>getFont()</tt>, 
with the <tt CLASS=literal>defaultFont</tt> parameter 
set to <tt CLASS=literal>null</tt>. 

<P CLASS=para>
Assuming the properties defined in the previous example, 
if you call the <tt CLASS=literal>getFont()</tt> 
method with <tt CLASS=literal>name</tt> set to <tt CLASS=literal>myPackage.myClass.menuFont</tt>, 
the return value is a 24-point, italic, TimesRoman <tt CLASS=literal>Font</tt> 
object. If called with <tt CLASS=literal>name</tt> 
set to <tt CLASS=literal>myPackage.myClass.inputFont</tt>, 
<tt CLASS=literal>getFont()</tt> returns a 12-point, 
plain Helvetica <tt CLASS=literal>Font</tt> object. 
If called with <tt CLASS=literal>myPackage.myClass.labelFont</tt> 
as <tt CLASS=literal>name</tt>, <tt CLASS=literal>getFont()</tt> 
returns <tt CLASS=literal>null</tt> because this user 
did not set the property <tt CLASS=literal>myPackage.myClass.labelFont</tt>. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public static Font getFont (String name, Font defaultFont) </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>getFont()</tt> method gets the 
font specified by the system property <tt CLASS=literal>name</tt>. 
If <tt CLASS=literal>name</tt> is not a valid system 
property, this version of <tt CLASS=literal>getFont()</tt> 
returns the <tt CLASS=literal>Font</tt> specified by <tt CLASS=literal>defaultFont</tt>. This version 
allows you to provide defaults in the event the user does not wish to provide 
his own font settings. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public static Font decode (String name)  <img src="gifs/bstar.gif" alt="(New)" border=0> </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>decode()</tt> method provides 
an explicit means to decipher font property settings, regardless of where 
the setting comes from. (The <tt CLASS=literal>getFont()</tt> 
method can decipher settings, but only if they're in the system properties 
file.) In particular, you can use <tt CLASS=literal>decode()</tt> 
to look up font settings in a resource file. The format of <tt CLASS=literal>name</tt> 
is the same as that used by <tt CLASS=literal>getFont()</tt>. 
If the contents of <tt CLASS=literal>name</tt> are 
invalid, a 12-point plain font is returned. To perform the equivalent of 
<tt CLASS=literal>getFont(`myPackage.myClass.menuFont`)</tt> 
without using system properties, see the following example. For a more extensive 
example using resource files, see Appendix A. </DL>
<P CLASS=para>
<DIV CLASS=screen>
<P>
<PRE>
// Java 1.1 only
InputStream is = instance.getClass().getResourceAsStream("propfile");
Properties p = new Properties();
try {
    p.load (is);
    Font f = Font.decode(p.getProperty("myPackage.myClass.menuFont"));
} catch (IOException e) {
    System.out.println ("error loading props...");
}
</PRE>
</DIV>

Miscellaneous methods

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><I CLASS=emphasis>public int hashCode ()  </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>hashCode()</tt> method returns 
a hash code for the font. This hash code is used whenever a <tt CLASS=literal>Font</tt> 
object is used as the key in a <tt CLASS=literal>Hashtable</tt>. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public boolean equals (Object o) </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>equals()</tt> method overrides 
the <tt CLASS=literal>equals()</tt> method 
of <tt CLASS=literal>Object</tt> to define equality for <tt CLASS=literal>Font</tt> 
objects. Two <tt CLASS=literal>Font</tt> objects are 
equal if their size, style, and name are equal. The following example demonstrates 
why this is necessary. </DL>
<P CLASS=para>
<DIV CLASS=screen>
<P>
<PRE>
Font a = new Font ("TimesRoman", Font.PLAIN, 10);
Font b = new Font ("TimesRoman", Font.PLAIN, 10);
// displays false since the objects are different objects
System.out.println (a == b);
// displays true since the objects have equivalent settings
System.out.println (a.equals (b));
</PRE>
</DIV>

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><I CLASS=emphasis>public String toString () </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>toString()</tt> method of <tt CLASS=literal>Font</tt> 
returns a string showing the current family, name, style, and size settings. 
For example: </DL>
<P CLASS=para>
<DIV CLASS=screen>
<P>
<PRE>
java.awt.Font[family=TimesRoman,name=TimesRoman,style=bolditalic,size=20]
</PRE>
</DIV>

</DIV>

</DIV>


<DIV CLASS=htmlnav>

<P>
<HR align=left width=515>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch02_08.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><a href="index.htm"><img src='gifs/txthome.gif' border=0 alt='Home'></a></td>
<td width=172 align=right valign=top><A HREF="ch03_02.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
<tr>
<td width=172 align=left valign=top>MediaTracker</td>
<td width=171 align=center valign=top><a href="index/idx_a.htm"><img src='gifs/index.gif' alt='Book Index' border=0></a></td>
<td width=172 align=right valign=top>FontMetrics</td>
</tr>
</table>
<hr align=left width=515>

<IMG SRC="gifs/smnavbar.gif" USEMAP="#map" BORDER=0> 
<MAP NAME="map"> 
<AREA SHAPE=RECT COORDS="0,0,108,15" HREF="../javanut/index.htm"
alt="Java in a Nutshell"> 
<AREA SHAPE=RECT COORDS="109,0,200,15" HREF="../langref/index.htm" 
alt="Java Language Reference"> 
<AREA SHAPE=RECT COORDS="203,0,290,15" HREF="../awt/index.htm" 
alt="Java AWT"> 
<AREA SHAPE=RECT COORDS="291,0,419,15" HREF="../fclass/index.htm" 
alt="Java Fundamental Classes"> 
<AREA SHAPE=RECT COORDS="421,0,514,15" HREF="../exp/index.htm" 
alt="Exploring Java"> 
</MAP>
</DIV>

</BODY>
</HTML>
